<%@ page language="java" contentType="text/html; charset=GB18030"
    pageEncoding="GB18030"%>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="org.json.JSONArray" %>
<%@ page import="org.json.JSONException" %>
<%@ page import="org.json.JSONObject" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.wenjie.shoprankmonitor.Util" %>
<%@ page import="com.wenjie.shoprankmonitor.GetTaobaoResult" %>
<%@ page import="com.wenjie.shoprankmonitor.User" %>
<%@ page import="com.wenjie.shoprankmonitor.Key_monitor_list" %>
<%@ page import="com.wenjie.shoprankmonitor.Shop_result" %>
<%@ page import="com.wenjie.shoprankmonitor.PMF" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>cron job</title>
</head>
<body>
<%
PersistenceManager pm = PMF.get().getPersistenceManager();
Date now = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(now);%><%=now.getTime() %><br /><%
cal.add(java.util.Calendar.HOUR_OF_DAY, -2);
now = cal.getTime();%><%=now.getTime() %><%
String query = "select from " + Key_monitor_list.class.getName() + " where monitor_time < " + now.getTime();
List<Key_monitor_list> keys = (List<Key_monitor_list>) pm.newQuery(query).execute();

for (Key_monitor_list key : keys) {
	%><p><%=key.getKey_name() %>:<%=key.getMonitor_time() %></p><%
	String keyword = key.getKey_name();
	String result = Util.getResult("http://gw.api.taobao.com/router/rest", GetTaobaoResult.testUserGet(keyword));
	%><br /><%=key.getMonitor_timeAsLong() %><%
	if (result != null) {
		try {
			JSONObject jb = new JSONObject(result);
			
			jb = jb.getJSONObject("items_get_response");
			    			
			jb = jb.getJSONObject("items");
			JSONArray jItems = jb.getJSONArray("item");		
			
			for(int i = 0; i < 5; i++) {
				
				JSONObject jItem = (JSONObject) jItems.get(i);
				
				String owner_nick = ((String) jItem.get("nick"));
				Long num_id = ((Long) jItem.get("num_iid"));
				Long owner_score = Long.parseLong((jItem.get("score")).toString());
				String title = ((String) jItem.get("title")).replace("<span class=H>", "").replace("</span>", "");
				String default_url = "http://item.tmall.com/item.htm?id=" + num_id;
				key.addResult(default_url, owner_nick, title, num_id, owner_score, i + 1);
				%><p>Get it!</p><%
			}
		} catch (JSONException e) {
			e.printStackTrace();
			%><p><%="question!!!" %></p><%
		}
	}
	key.setMonitor_time();
	key.setMessage_content("Result Udated!");
}
pm.close();
%><p>end</p>
</body>
</html>